/*
 * @Author: 兑震鹏
 * @Description: 
 * @Date: 2020-05-26 18:51:19
 * @LastEditTime: 2020-05-27 16:07:19
 */
import * as cesium from 'cesium/Cesium'
const Cesium =cesium
export default class Radar {
  constructor(mapControl) {
    this.mapControl = mapControl;
    this.dataList = [];
    this.install();
  }
  addCircleRipple(viewer, data){
    var r1 = data.minR, r2 = data.minR;

    function changeR1() { //这是callback，参数不能内传
      r1 = r1 + data.deviationR;
      if (r1 >= data.maxR) {
        r1 = data.minR;
      }

      return r1;
    }
    function changeR2() {
      r2 = r2 + data.deviationR;
      if (r2 >= data.maxR) {
        r2 = data.minR;
      }
      return r2;
    }
    let obj1 = viewer.entities.add({
      id: data.id,
      name: "",
      position: Cesium.Cartesian3.fromDegrees(data.lon, data.lat, data.height),
      ellipse: {
        semiMinorAxis: new Cesium.CallbackProperty(changeR1, false),
        semiMajorAxis: new Cesium.CallbackProperty(changeR1, false),
        height: data.height,
        material: new Cesium.ImageMaterialProperty({
          image: data.imageUrl,
          repeat: new Cesium.Cartesian2(1.0, 1.0),
          transparent: true,
          color: new Cesium.CallbackProperty(function () {
            var alp = 1 - r1 / data.maxR;
            return Cesium.Color.WHITE.withAlpha(alp)  //entity的颜色透明 并不影响材质，并且 entity也会透明哦
          }, false)
        })
      }
    });
    this.dataList.push(obj1);
    setTimeout( () =>{
      let obj2 = viewer.entities.add({
        name: "",
        position: Cesium.Cartesian3.fromDegrees(data.lon, data.lat, data.height),
        ellipse: {
          semiMinorAxis: new Cesium.CallbackProperty(changeR2, false),
          semiMajorAxis: new Cesium.CallbackProperty(changeR2, false),
          height: data.height,
          material: new Cesium.ImageMaterialProperty({
            image: data.imageUrl,
            repeat: new Cesium.Cartesian2(1.0, 1.0),
            transparent: true,
            color: new Cesium.CallbackProperty(function () {
              var alp = 1;
              alp = 1 - r2 / data.maxR;
              return Cesium.Color.WHITE.withAlpha(alp)
            }, false)
          })
        }
      });
      this.dataList.push(obj2);
    }, data.eachInterval)
  }
  install() {
    
  
    // var lon = 119.18950276876987;
    // var lat = 36.700223922438724;
    // addCircleRipple(viewer, { //默认只绘制两个圆圈叠加 如遇绘制多个，请自行源码内添加。
    //   id: "111",
    //   lon: lon,
    //   lat: lat,
    //   height: 0,
    //   maxR: 3000,
    //   minR: 0,//最好为0
    //   deviationR: 10,//差值 差值也大 速度越快
    //   eachInterval: 2000,//两个圈的时间间隔
    //   imageUrl: "./images/redCircle2.png"
    // });
  }
  addCircular(newConfig){
    console.log('ccccccccccccccccccccccc')
    let defaultConfig = {
      lon: 0,
      lat: 0,
      height: 1,
      maxR: 50,
      minR: 0,//最好为0
      deviationR: .1,//差值 差值也大 速度越快
      eachInterval: 900,//两个圈的时间间隔
      imageUrl: "./images/redCircle2.png"
    };
    let config = Object.assign(defaultConfig,newConfig)
    let viewer = this.mapControl.viewer;
    this.addCircleRipple(viewer,config);
  }
  removeAllCircular() {
    // console.log(this.dataList)
    let viewer = this.mapControl.viewer;
    this.dataList.forEach(item => {
      viewer.entities.remove(item);
    })
    this.dataList = [];
  }
  uninstall() {
  
  }
}